package com.kgc.ssm.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.kgc.ssm.shiro.UserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

//Shiro的配置类
@Configuration
public class ShiroConfig {
    // 自定义 Realm （保持不变）
    @Bean
    public Realm realm() {
        UserRealm userRealm = new UserRealm();
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher("MD5");
        matcher.setHashIterations(2);
        userRealm.setCredentialsMatcher(matcher);
        return userRealm;
    }

    // 配置 RememberMe Cookie
    @Bean
    public SimpleCookie rememberMeCookie() {
        SimpleCookie rememberMeCookie = new SimpleCookie("rememberMe");
        rememberMeCookie.setMaxAge(7 * 24 * 60 * 60);
        rememberMeCookie.setHttpOnly(true);
        return rememberMeCookie;
    }

    // 配置 RememberMe 管理器
    @Bean
    public CookieRememberMeManager rememberMeManager() {
        CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
        rememberMeManager.setCookie(rememberMeCookie());
        rememberMeManager.setCipherKey(Base64.decode("2AvVhdsgUs0FSA3SDFAdag=="));
        return rememberMeManager;
    }

    // 配置 Shiro 的安全管理器
    @Bean
    public DefaultWebSecurityManager securityManager(Realm realm, SessionManager sessionManager, CookieRememberMeManager rememberMeManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        securityManager.setSessionManager(sessionManager);
        securityManager.setRememberMeManager(rememberMeManager);
        return securityManager;
    }

    // 配置 Shiro 的过滤器
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/sysUsers/login", "anon");
        map.put("/register.html", "anon");
        map.put("/sysUsers/register", "anon");
        map.put("/static/**", "anon");
        map.put("/**", "user"); // 修改为 "user" 以支持 RememberMe
        bean.setFilterChainDefinitionMap(map);
        bean.setLoginUrl("/login.html");
        return bean;
    }

    // 配置 Session 管理器（保持不变）
    @Bean
    public SessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setGlobalSessionTimeout(3600000);
        sessionManager.setDeleteInvalidSessions(true);
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    // 配置 Shiro 方言（保持不变）
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }
}